import {_decorator, AnimationClip, Component, Node, SkeletalAnimation, SkeletalAnimationState, Vec3} from 'cc';
import {AudioManager} from "db://assets/scripts/common/AudioManager";
import {GameManager} from "db://assets/scripts/game/GameManager";
import {AudioPaths} from "db://assets/scripts/common/AudioPaths";
import {PlayerAnimationType} from "db://assets/scripts/game/role/player/PlayerAnimationType";
import {PlayerManager} from "db://assets/scripts/game/role/player/PlayerManager";
import {GameState} from "db://assets/scripts/game/GameState";

const {ccclass, property} = _decorator;

@ccclass('PlayerModelManager')
export class PlayerModelManager extends Component {
    @property(Node)
    public ndSocketLoose: Node = null!//弓箭发射的节点

    @property(Node)
    public ndSocketHand: Node = null!//手节点

    @property(Node)
    public ndArrow: Node = null!;//攻击时候展示的弓箭

    @property(SkeletalAnimation)
    public aniComPlayer: SkeletalAnimation = null!;//动画播放组件

    public looseEulerAngles: Vec3 = new Vec3();//射箭时的角度
    public isAniPlaying: boolean = false;//当前动画是否正在播放

    //是否正在跑
    public get isRunning() {
        return this._aniType === PlayerAnimationType.RUN && this.isAniPlaying === true;
    }

    //是否待机
    public get isIdle() {
        return this._aniType === PlayerAnimationType.IDLE && this.isAniPlaying === true;
    }

    //是否正在攻击
    public get isAttacking() {
        return this._aniType === PlayerAnimationType.ATTACK && this.isAniPlaying === true;
    }

    private _aniType: string = "";//动画类型
    private _aniState: SkeletalAnimationState = null!;//动画播放状态
    private _stepIndex: number = 0;//脚步

    start() {
        // [3]
    }

    public init() {
        this.hideArrow();
    }

    /**
     * attack帧事件:箭射出去的时候触发
     * @returns
     */
    public onFrameAttackLoose() {
        if (GameManager.getInstance().gameStateProxy.getGameState() === GameState.OVER
            || GameManager.getInstance().gameStateProxy.getGameState() === GameState.PAUSE) {
            return;
        }

        this.looseEulerAngles = this.node.parent?.eulerAngles as Vec3;
        PlayerManager.getInstance().throwArrowToEnemy();
        this.ndArrow.active = false;
        // console.log("looseEulerAngles", this.looseEulerAngles);
    }

    /**
     * run帧事件：脚落地的时候播放音效
     *
     * @memberof PlayerModel
     */
    public onFrameRun() {
        this._stepIndex = this._stepIndex === 0 ? 1 : 0;
        AudioManager.getInstance().playSound(this._stepIndex === 0 ? AudioPaths.footStep1Sound : AudioPaths.footStep2Sound);
    }

    /**
     * attack帧事件: 拉弓的时候触发
     */
    public onFrameAttackDraw() {
        this.ndArrow.active = true;
    }

    /**
     * 隐藏模型手中默认的箭
     */
    public hideArrow() {
        this.ndArrow.active = false;
    }

    /**
     * 播放玩家动画
     *
     * @param {string} aniType 动画类型
     * @param {boolean} [isLoop=false] 是否循环
     * @param {Function} [callback] 回调函数
     * @param {number} [callback] 调用播放动画的位置，方便用于测试
     * @returns
     * @memberof Player
     */
    public playAni(aniType: string, isLoop: boolean = false, callback?: Function, pos?: number) {
        // console.log("playerAniType", aniType, "curAniType", this.aniType, "pos", pos);
        this._aniState = this.aniComPlayer?.getState(aniType) as SkeletalAnimationState;

        if (this._aniState && this._aniState.isPlaying) {
            return;
        }

        this._aniType = aniType;

        if (this._aniType !== PlayerAnimationType.ATTACK) {
            this.hideArrow();
        }

        this.aniComPlayer?.play(aniType);

        this.isAniPlaying = true;

        if (this._aniState) {
            if (isLoop) {
                this._aniState.wrapMode = AnimationClip.WrapMode.Loop;
            } else {
                this._aniState.wrapMode = AnimationClip.WrapMode.Normal;
            }

            switch (aniType) {
                case PlayerAnimationType.ATTACK:
                    // this._aniState.speed = GameManager.gameSpeed * GameManager.scriptPlayer.curAttackSpeed;
                    // GameManager.scriptPlayer.hideRunSmoke();
                    break;
                case PlayerAnimationType.RUN:
                    // console.log("run>>>>>>");
                    // this._aniState.speed = GameManager.gameSpeed * (GameManager.scriptPlayer.curMoveSpeed / GameManager.scriptPlayer.playerBaseInfo.moveSpeed);
                    // GameManager.scriptPlayer.playRunSmoke();
                    PlayerManager.getInstance().playRunSmoke();
                    break;
                case PlayerAnimationType.IDLE:
                    this._aniState.speed = GameManager.getInstance().gameSpeedProxy.getGameSpeed();
                    break;
                default:
                    this._aniState.speed = GameManager.getInstance().gameSpeedProxy.getGameSpeed();
                    break;
            }
        }

        if (!isLoop) {
            this.aniComPlayer.once(SkeletalAnimation.EventType.FINISHED, () => {
                this.isAniPlaying = false;
                callback && callback();
            })
        }
    }
}

